﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Swift;
using CB.Logic;
using DG.Tweening;

namespace CB.Client
{
    public abstract class BlockIcon<T> : UIContent where T : Block
    {
        [SerializeField] protected Image Icon;

        private Button IconBtn;
        public abstract T Block { get; set; }

        private void Awake()
        {
            IconBtn = Icon.GetComponent<Button>();
            IconBtn.onClick.AddListener(TryMove);
        }

        public abstract void SetData(T block);        
        public void InitMaterial()
        {
            Icon.material.SetFloat("_Fade", 1f);
        }

        private bool StartMove = false;
        private float MoveRangeX => (transform as RectTransform).sizeDelta.x;
        private float MoveRangeY => (transform as RectTransform).sizeDelta.y;
        private readonly float MoveTime = 0.3f;

        public void DoMove(Direction dir, Action callBack)
        {
            switch (dir)
            {
                case Direction.None:
                    break;
                case Direction.Left:
                    MoveLeft(callBack);
                    break;
                case Direction.Right:
                    MoveRight(callBack);
                    break;
                case Direction.Up:
                    MoveUp(callBack);
                    break;
                case Direction.Down:
                    MoveDown(callBack);
                    break;
                default:
                    break;
            }
        }

        private void MoveLeft(Action callBack)
        {
            transform.DOLocalMoveX(transform.localPosition.x - MoveRangeX, MoveTime).onComplete += () => callBack();
        }
        private void MoveRight(Action callBack)
        {
            transform.DOLocalMoveX(transform.localPosition.x + MoveRangeX, MoveTime).onComplete += () => callBack();
        }
        private void MoveUp(Action callBack)
        {
            transform.DOLocalMoveY(transform.localPosition.y + MoveRangeY, MoveTime).onComplete += () => callBack();
        }
        private void MoveDown(Action callBack)
        {
            transform.DOLocalMoveY(transform.localPosition.y - MoveRangeY, MoveTime).onComplete += () => callBack();
        }

        public void StartDissolve(float dissolveTime)
        {
            Icon.material.DOFloat(0f, "_Fade", dissolveTime).onComplete += () => Icon.material.DOFloat(1f, "_Fade", dissolveTime);
        }

        public Action<T> Action_ClickBlock;
        /// <summary>
        /// 通过点击尝试移动
        /// </summary>
        private void TryMove()
        {
            Action_ClickBlock?.Invoke(Block);
        }
    }
}